﻿/*状态机*/
var preload = {
	_list: {},
	_cache: window,
	_flag: {},
	setCache: function(cache) {
		this._cache = cache;
	},
	push: function(name, params) {
		this._list[name] = params;
		this._flag[name] = false;
	},
	run: function(lastFn) {
		var me = this;
		var list = me._list;
		if(!$.isEmptyObject(list)) {
			for(var li in list) {
				invoke(li, list[li]);

			}
		} else {
			lastFn();
		}

		function invoke(name, params) {
			$.post(params.url, params.data, function(data) {
				me._cache[name] = data;
				me._flag[name] = true;
				params.callback && params.callback(data);
				check() && lastFn();
			});
		}

		function check() {
			var flags = me._flag;
			for(var i in flags) {
				if(!flags[i]) {
					return false;
				}
			}
			return true;
		}
	}
};